<?php

class Damages_BackendController extends Zend_Controller_Action {

  const HOMEPAGE_URL = 'http://vw-group.pl/';

  private $params;
  private $authMethods = array(
      'login', 'logout', 'check.session'
  );

  public function init() {
    if (!$this->getRequest()->isXmlHttpRequest()) {
      $this->_helper->redirector->gotoUrlAndExit(self::HOMEPAGE_URL);
      return;
    }
    $post = $this->getRequest()->getParams();
    $this->params = (array_key_exists('params', $post)) ? new Damages_DynamicClass($post['params']) : null;
    $this->jsonData();
    if ($this->params === null)
      throw new Damages_Exception_App(Damages_Codes::ERROR_NO_PARAMS);
    if ($this->params->app === null)
      throw new Damages_Exception_App(Damages_Codes::ERROR_NO_APP);
    $action = $this->getRequest()->getActionName();
    Damages_Auth::getInstance();
    Damages_Auth::getInstance()->setApp(new Damages_Methods_Data_App($this->params->app));
    if (!in_array($action, $this->authMethods) && !Damages_Auth::getInstance()->isAuth())
      throw new Damages_Exception_App(Damages_Codes::INVALID_SESSION);
    else if (!in_array($action, $this->authMethods))
      Damages_Auth::getInstance()->setUserPermissions();
  }

  public function jsonData() {
    $this->_helper->layout->disableLayout();
    $action = $this->getRequest()->getActionName();
    $this->_helper->viewRenderer->setNoRender(true);
    $this->_helper->contextSwitch()
            ->addActionContext($action, 'json')
            ->setAutoJsonSerialization(true)
            ->initContext('json');
    $this->getRequest()->setParam('format', 'json');
  }

  public function checkSessionAction() {
    $request = new Damages_Methods_Request($this->params);
    $this->view->response = $request->process();
  }

  public function loginAction() {
    $request = new Damages_Methods_Request_Login($this->params);
    $this->view->response = $request->process();
  }

  public function logoutAction() {
    $request = new Damages_Methods_Request_Logout($this->params);
    $this->view->response = $request->process();
  }

  public function getDictionariesAction() {
    $request = new Damages_Methods_Request_GetDictionaries($this->params);
    $this->view->response = $request->process();
  }

  public function getPermissionManagmentDataAction() {
    $request = new Damages_Methods_Request_GetPermissionManagmentData($this->params);
    $this->view->response = $request->process();
  }

  public function getEmailTemplatesAction() {
    $request = new Damages_Methods_Request_GetEmailTemplates($this->params);
    $this->view->response = $request->process();
  }

  public function editEmailTemplateAction() {
    $request = new Damages_Methods_Request_Edit_EmailTemplate($this->params);
    $this->view->response = $request->process();
  }

  public function deleteDamageScaleAction() {
    $request = new Damages_Methods_Request_Delete_DamageScale($this->params);
    $this->view->response = $request->process();
  }

  public function deleteDamageSpotAction() {
    $request = new Damages_Methods_Request_Delete_DamageSpot($this->params);
    $this->view->response = $request->process();
  }

  public function deleteDamageTypeAction() {
    $request = new Damages_Methods_Request_Delete_DamageType($this->params);
    $this->view->response = $request->process();
  }

  public function deleteDiscardAction() {
    $request = new Damages_Methods_Request_Delete_Discard($this->params);
    $this->view->response = $request->process();
  }

  public function deleteDiscountAction() {
    $request = new Damages_Methods_Request_Delete_Discount($this->params);
    $this->view->response = $request->process();
  }

  public function deletePerpetratorAction() {
    $request = new Damages_Methods_Request_Delete_Perpetrator($this->params);
    $this->view->response = $request->process();
  }

  public function editDamageScaleAction() {
    $request = new Damages_Methods_Request_Edit_DamageScale($this->params);
    $this->view->response = $request->process();
  }

  public function editDamageSpotAction() {
    $request = new Damages_Methods_Request_Edit_DamageSpot($this->params);
    $this->view->response = $request->process();
  }

  public function editDamageTypeAction() {
    $request = new Damages_Methods_Request_Edit_DamageType($this->params);
    $this->view->response = $request->process();
  }

  public function editDiscardAction() {
    $request = new Damages_Methods_Request_Edit_Discard($this->params);
    $this->view->response = $request->process();
  }

  public function editDiscountAction() {
    $request = new Damages_Methods_Request_Edit_Discount($this->params);
    $this->view->response = $request->process();
  }

  public function editBrandAction() {
    $request = new Damages_Methods_Request_Edit_Brand($this->params);
    $this->view->response = $request->process();
  }

  public function editPerpetratorAction() {
    $request = new Damages_Methods_Request_Edit_Perpetrator($this->params);
    $this->view->response = $request->process();
  }

  public function newDamageScaleAction() {
    $request = new Damages_Methods_Request_New_DamageScale($this->params);
    $this->view->response = $request->process();
  }

  public function newDamageSpotAction() {
    $request = new Damages_Methods_Request_New_DamageSpot($this->params);
    $this->view->response = $request->process();
  }

  public function newDamageTypeAction() {
    $request = new Damages_Methods_Request_New_DamageType($this->params);
    $this->view->response = $request->process();
  }

  public function newDiscardAction() {
    $request = new Damages_Methods_Request_New_Discard($this->params);
    $this->view->response = $request->process();
  }

  public function newDiscountAction() {
    $request = new Damages_Methods_Request_New_Discount($this->params);
    $this->view->response = $request->process();
  }

  public function newPerpetratorAction() {
    $request = new Damages_Methods_Request_New_Perpetrator($this->params);
    $this->view->response = $request->process();
  }

  public function editProcessPermissionsAction() {
    $request = new Damages_Methods_Request_Edit_ProcessPermissions($this->params);
    $this->view->response = $request->process();
  }

  public function editFieldsPermissionsAction() {
    $request = new Damages_Methods_Request_Edit_FieldsPermissions($this->params);
    $this->view->response = $request->process();
  }

  public function getDamagesListAction() {
    $request = new Damages_Methods_Request_GetDamagesList($this->params);
    $this->view->response = $request->process();
  }

  public function getCarDataAction() {
    $request = new Damages_Methods_Request_GetCarData($this->params);
    $this->view->response = $request->process();
  }

  public function newCarDamageAction() {
    $request = new Damages_Methods_Request_New_CarDamage($this->params);
    $this->view->response = $request->process();
  }

  public function getCarDamageAction() {
    $request = new Damages_Methods_Request_GetCarDamage($this->params);
    $this->view->response = $request->process();
  }

  public function getExpirationDictsAction() {
    $request = new Damages_Methods_Request_GetExpirationsDicts($this->params);
    $this->view->response = $request->process();
  }

  public function editExpirationDictsAction() {
    $request = new Damages_Methods_Request_Edit_ExpirationsDicts($this->params);
    $this->view->response = $request->process();
  }

  public function getDiscountDataAction() {
    $request = new Damages_Methods_Request_GetDiscountData($this->params);
    $this->view->response = $request->process();
  }

  public function sendPerpetratorEmailAction() {
    $request = new Damages_Methods_Request_SendPerpetratorEmail($this->params);
    $this->view->response = $request->process();
  }

  public function sendDealerEmailAction() {
    $request = new Damages_Methods_Request_SendDealerEmail($this->params);
    $this->view->response = $request->process();
  }

  public function setCarDamageLsAction() {
    $request = new Damages_Methods_Request_SetCarDamageLs($this->params);
    $this->view->response = $request->process();
  }

  public function setCarDamageDiscardAction() {
    $request = new Damages_Methods_Request_SetCarDamageDiscard($this->params);
    $this->view->response = $request->process();
  }

  public function setCarDataDealerAction() {
    $request = new Damages_Methods_Request_SetCarDataDealer($this->params);
    $this->view->response = $request->process();
  }

  public function getBrandDealersAction() {
    $request = new Damages_Methods_Request_GetBrandDealers($this->params);
    $this->view->response = $request->process();
  }

  public function dealerCarDamageClaimAction() {
    $request = new Damages_Methods_Request_DealerCarDamageClaim($this->params);
    $this->view->response = $request->process();
  }

  public function setCarRelatedDamageAction() {
    $request = new Damages_Methods_Request_SetCarRelatedDamage($this->params);
    $this->view->response = $request->process();
  }

  public function editCarDamageAction() {
    $request = new Damages_Methods_Request_Edit_CarDamage($this->params);
    $this->view->response = $request->process();
  }

  public function changePasswordAction() {
    $request = new Damages_Methods_Request_ChangePassword($this->params);
    $this->view->response = $request->process();
  }

  public function setCarDamageStatusAction() {
    $request = new Damages_Methods_Request_SetCarDamageStatus($this->params);
    $this->view->response = $request->process();
  }

  public function sendPerpetratorEmailsAction() {
    $request = new Damages_Methods_Request_SendPerpetratorEmails($this->params);
    $this->view->response = $request->process();
  }

  public function sendCarValueInquiryAction() {
    $request = new Damages_Methods_Request_SendCarValueInquiry($this->params);
    $this->view->response = $request->process();
  }

  public function getUsersAction() {
    $request = new Damages_Methods_Request_GetUsers($this->params);
    $this->view->response = $request->process();
  }

  public function getDealersAction() {
    $request = new Damages_Methods_Request_GetDealers($this->params);
    $this->view->response = $request->process();
  }

  public function getPermissionsAction() {
    $request = new Damages_Methods_Request_GetPermissions($this->params);
    $this->view->response = $request->process();
  }

  public function resetPasswordAction() {
    $request = new Damages_Methods_Request_ResetPassword($this->params);
    $this->view->response = $request->process();
  }

  public function saveUserAction() {
    $request = new Damages_Methods_Request_Edit_User($this->params);
    $this->view->response = $request->process();
  }

}